use libactionkv::ActionKV; //属于该项目但是需要像其他包一样对待

//cfg属性注解，可以让win用户在帮助文档中看到正确的文件扩展名
#[cfg(target_os = "windows")]
const USAGE: &str = "
Usage:
    akv_mem.exe FILE get KEY
    akv_mem.exe FILE delete KEY
    akv_mem.exe FILE insert KEY VALUE
    akv_mem.exe FILE update KEY VALUE
";

#[cfg(not(target_os = "windows"))] //非win用户帮助文档
const USAGE: &str = "
Usage:
    akv_mem FILE get KEY
    akv_mem FILE delete KEY
    akv_mem FILE insert KEY VALUE
    akv_mem FILE update KEY VALUE
";

fn main(){
    let args: Vec<String>=std::env::args().collect();
    let fname = args.get(1).expect(&USAGE);
    let action = args.get(2).expect(&USAGE).as_ref();
    let key = args.get(3).expect(&USAGE).as_ref();
    let maybe_value = args.get(4);

    let path = std::path::Path::new(&fname);
    let mut store = ActionKV::open(path).expect("unable to open file"); //打开文件
    store.load().expect("unable to load data"); //加载数据创建索引

    match action{
        "get" => match store.get(key).unwrap() {
            None=> eprintln!("{:?} not found",key),
            Some(value) => println!("{:?}",value),
        },

        "delete"=> store.delete(key).unwrap(),

        "insert" => {
            let value = maybe_value.expect(&USAGE).as_ref();
            store.insert(key,value).unwrap()
        }

        "update" =>{
            let value = maybe_value.expect(&USAGE).as_ref();
            store.update(key,value).unwrap()
        }

        _ => eprintln!("{}",&USAGE),
    }
}